<?php
/* functions.new-finger.php */

/* create new finger */
function Finger_create($for, $why) {
  global $newFingerId;
  $error_msg = "";
  // Check if user is logged in again, sometimes the session doesn't work very fine.
  if (!isset($_SESSION['ses_id']) OR intval($_SESSION['ses_id']) == 0)
    die('A problem ocurred, please go back and try again and don\'t forget to use the feedback form to report it!');
  // Check if the finger exists
  $sql_cmd = "select id, `for` from fingers where upper(`for`) = '".strtoupper(mysql_real_escape_string($for))."'";
  $exists = Db::getInstance()->GetRow($sql_cmd);
  if (isset($exists['id']) AND intval($exists['id']) > 0) {
    return "<p>The finger you have entered already exists, you can see it <a href=\"".Finger_link($exists['for'], $exists['id'])."\">here</a>.</p>";
  }
  // Create the finger
  $sql_cmd = "INSERT INTO fingers(`for`, `id_user`, `timestamp`) values('".mysql_real_escape_string($for)."',".intval($_SESSION['ses_id']).", '".time()."')";
  $add_finger = Db::getInstance()->Execute($sql_cmd);
  // Save the comment
  $comment = new Comment();
  $comment->id_finger = Db::getInstance()->Insert_ID();
  $comment->id_user = intval($_SESSION['ses_id']);
  $comment->comment = $why;
  $comment->type = Comment::TYPE_FINGER_UP;
  $comment->timestamp = time();
  $comment->save();
  // Get the last inserted id
  $newFingerId = $comment->id_finger;
  // Reset New Finger Form
  unset($_SESSION['new_finger_for']);
  unset($_SESSION['new_finger_why']);
  return $error_msg;
}
?>